<?php namespace addons\links\controller;

use addons\links\model\Links;
use houdunwang\request\Request;
use module\HdController;

/**
 * 友链管理
 * Class Admin
 * @package addons\links\controller
 */
class Admin extends HdController
{

	/**
	 * 友链首页数据
	 * @return mixed
	 */
	public function index ()
	{
		//测试能否正常输出
		//echo 'links index';
		//加载模板文件
		$field = Db::table('addons_links')->orderBy('links_orderby','desc')->get();
		//p($field);
		return $this->template('',compact('field'));
	}

	/**
	 * 友链添加/编辑
	 * @return array|mixed
	 */
	public function post ( )
	{
		$links_id = Request::get( 'links_id' );
		$model    = Links::find( $links_id ) ? : new  Links();
		if ( IS_POST ) {
			$model->save( Request::post() );

			return $this->setRedirect( url( 'admin.index' ) )->success( '操作成功' );
		}

		return $this->template( '' , compact( 'model' ) );

	}

	/**
	 * 友链删除
	 * @return array
	 */
	public function del()
	{
		$links_id = Request::get( 'links_id' );
		//p($links_id);
		$model = Links::find($links_id);
		$model->destory();
		return $this->setRedirect(url('admin.index'))->success('操作成功');
	}
}